package org.demo.controller;

import org.demo.common.Result;
import org.demo.utils.AliOssUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@CrossOrigin(origins = "*")
@RestController
public class FileUploadController {
//    @PostMapping("/upload")
//    public Result<String> upload(MultipartFile file) throws Exception {
//        //把文件内容储存到本地磁盘上
//        String orginalFilename = file.getOriginalFilename();
//        //保证文件名字唯一
//        String filename= UUID.randomUUID().toString()+orginalFilename.substring(orginalFilename.lastIndexOf("."));
//
////        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
//
//
//        //存储本地磁盘
//        file.transferTo(new File("G:\\bishe\\intermediary-management\\files\\"+filename));
//        String url = "G:\\bishe\\intermediary-management\\files\\"+filename;
////        return Result.success("url");
//
//        return Result.success(url);
//    }
@PostMapping("/upload")
public Result<String> upload(MultipartFile file) throws Exception {
    String originalFilename = file.getOriginalFilename();
    String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
    String filename = UUID.randomUUID() + suffix;

    // 保存到本地
    File dest = new File("G:\\bishe\\intermediary-management\\files\\" + filename);
    file.transferTo(dest);

    // 返回 HTTP 访问路径（根据你的实际域名修改）
    String url = "http://localhost:8080/files/" + filename;
    return Result.success(url);
}
}

